package net.dgg.zz.production.singleRejectedBack.controller;

import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.iboss.base.util.poi.ExcelUtil;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.production.singleRejectedBack.service.ZzChargeBackRejectedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退单驳回
 * 孙上峰
 */
@Controller
@RequestMapping("/zzsc/chargebackre_jected")
public class ZzChargeBackRejectedController extends DggBaseController {

    @Autowired
    private ZzChargeBackRejectedService zzChargeBackRejectedService;
    @Autowired
    protected DesUtil desUtil;
    @Autowired
    protected SessionManager sessionManager;
    @Autowired
    private TranslateUtil translateUtil;

    /**
     * 初始化界面
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "zzsc/rejected/chargeback_rejected_list";
    }


    /**
     * 退单驳回分页列表
     *
     * @param params {codeName:模块名称}
     * @return
     */
    @RequestMapping(value = "/query_chargeback_rejected")
    @ResponseBody
    public DataTableResponse queryChargeBackRejected(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        UserEntity userEntity = sessionManager.getCurrentUser();
        params.put("start", (r.getPage() - 1) * r.getLength());
        params.put("limit", r.getLength());
        params.put("status", ScProductConstant.SC_ORDER_CHARGEBACK_TDBH); //退单驳回
        params.put("userEntity", userEntity.getId()); //当前登录人auditorId
        if (!net.dgg.iboss.base.util.StringUtils.isEmptyObj(params.get("customerPhone"))) {
            params.put("customerPhone", desUtil.encrypt(params.get("customerPhone").toString()));
        }
        Map fileMap = new HashMap();
        fileMap.put("receiveOrderDate", "receiveOrderDate");
        fileMap.put("lastRemarkTime", "lastRemarkTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<Map> listPage = zzChargeBackRejectedService.queryListPage(params);
        String[] transColum = {"originCode", "originType", "businessTypeId", "businessProductId"};
        listPage = translateUtil.translateList(transColum, listPage);
        String[] hideColum = {"customerPhone"};
        listPage = desUtil.decryptListMap(hideColum, listPage);
        CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
        Integer count = Integer.parseInt(params.get("count").toString());
        return new DataTableResponse(r.getDraw(), count, count, listPage, "");
    }

    /**
     * 打开退单恢复办理界面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/restore_page")
    public String openRestore(Long id, Model model) {
        model.addAttribute("id", id);
        return "zzsc/rejected/restore_page";
    }

    /**
     * 退单恢复办理
     *
     * @param id
     * @return
     */
    @AuthOpt(code = "SC_GENERAL_RESTORE")
    @RequestMapping("/restore_order")
    @ResponseBody
    public Object restoreOrder(Long id) {
        try {
            this.zzChargeBackRejectedService.restoreOrder(id);
            return this.getSuccessResponse("success!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * 导出
     *
     * @param params
     * @param request
     * @param response
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ResponseBody
    public void POIExcel(@RequestParam Map params, HttpServletRequest request, HttpServletResponse response) {
        DataTableRequest r = DataTableUtils.getParam(request);
        UserEntity userEntity = sessionManager.getCurrentUser();
        params.put("start", r.getStart());
        params.put("limit", r.getLength());
        params.put("status", ScProductConstant.SC_ORDER_CHARGEBACK_TDBH); //退单驳回
        params.put("producerId", userEntity.getId()); //当前登录人auditorId
        if (!net.dgg.iboss.base.util.StringUtils.isEmptyObj(params.get("customerPhone"))) {
            params.put("customerPhone", desUtil.encrypt(params.get("customerPhone").toString()));
        }
        List<Map> listPage = zzChargeBackRejectedService.queryListPage(params);
        String[] transColum = {"originCode", "originType", "businessTypeId", "businessProductId"};
        ;
        listPage = translateUtil.translateList(transColum, listPage);
        String[] hideColum = {"customerPhone"};
        listPage = desUtil.decryptListMap(hideColum, listPage);
        CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
        String fileName = "退单驳回" + ".xls";
        String[] title = {"生产订单编号", "订单编号", "当前完成节点", "客户", "业态", "业务类型", "订单产品", "节点截止时间", "生产到期时间", "流程人员", "流程部门", "节点更新人"};
        String[] key = {"scProductOrderNo", "scOrderNo", "latestEndNodeName", "customerName", "businessTypeId", "businessProductId", "productName", "nodeEndTime", "orderEndTime", "flowUserName", "flowUserOrgName", "latestEndUserName"};
        ExcelUtil.createN(fileName, title, key, listPage, response);
    }
}
